<?php

namespace app\api\controller;

use app\common\lib\Arr;
use app\common\lib\Show;
use app\common\lib\Time;
use think\response\Json;

class WorkerServer
{

    //审核中的零活process_status=0，开工时间到自动变为已过期
    public function sendTimeOutWorkMessage(){
        ini_set('max_execution_time',0);
        ini_set('memory_limit', -1);

        $workBis = new \app\common\business\Work();
        $workModel = new \app\common\model\mysql\Work();

        $condition = [
            ['process_status','in',[0]]
        ];
        $workList = $workBis->getListByWhere($condition,0,0,'id,name,process_status,is_show,start_time,end_time,valuation,duration');
        if (!empty($workList)){
            $workListTimeOut = []; //过期

            foreach ($workList as $k=>$v){
                /*$v['process_status'] == 0 && */$workListTimeOut[] = $v;
            }
            unset($workList);

            if (!empty($workListTimeOut)){
                $updateTimeOutIds = [];
                foreach ($workListTimeOut as $k=>$v){
                    //当前时间晚于开工时间，记录过期work的id
                    time() >= $v['start_time'] && $updateTimeOutIds[] = $v['id'];
                }

                if (!empty($updateTimeOutIds)){
                    $updateTimeOutData = [
                        'process_status'=>4,
                        'update_time'=>time()
                    ];
                    $workModel->updateByIds($updateTimeOutIds,$updateTimeOutData);
                }
            }
        }

        echo date('Y-m-d H:i:s').'_work_success;';
    }

    public function sendTimeOutOrderMessage(){
        ini_set('max_execution_time',0);
        ini_set('memory_limit', -1);

        //关闭订单
        $orderBis = new \app\common\business\Order();
        $orderList = $orderBis->closeOrder();
        if(!empty($orderList)){
            //取消零活
            $workIdArr = array_column($orderList,'work_id');
            $workBis = new \app\common\business\Work();
            $workBis->cancelWork($workIdArr);
        }
        echo date('Y-m-d H:i:s').'_order_success;';
    }
}